﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Corona.TagToolkit;
using System.IO;
using System.Threading;
using System.Data.Common;
using System.Text.RegularExpressions;
namespace Corona.TagPresent
{
    public partial class Window1
    {

        private void onTagClick(object sender, TagEventArgs e)
        {
            TagInfo info = e.TagInfo;
            info.IsSelected = true;
            //todo 移动至中间
            Thread searchTread = new Thread(delegate()
                {
                    //启动新线程搜索相关标签
                    Thread relateTagThread = new Thread(delegate()
                    {
                        TagJar tagjar = new TagJar();
                        tagjar.Add(new Tag(info.TagType, info.TagValue), (long)info.Width);
                        TagJar resultTagJar = CoronaSupport.GetTagJar(tagjar, 0.8);
                        foreach (Pair<Tag, long> tagWeightPair in resultTagJar.Relations)
                        {
                            //todo 实例化标签
                        }
                        
                    });
                    Resource<File>[] resultResource = CoronaSupport.getResources(CoronaSupport.Search(new Tag(info.TagType, info.TagValue)));
                    foreach (Resource<File> resource in resultResource)
                    {
                        //todo 在文件显示面板上显示
                    }
                });
        }
        private void onAddTagButtonClick(object sender, EventArgs e)
        {
            string searchText = searchTextBox.Text;

            //todo 如果有类型在此获得类型信息

            if (searchText.IndexOf(',') != 0)
            {
                
                string[] tagStringArray = searchText.Split(',');
                foreach (string tagString in tagStringArray)
                {
                    //todo 实例化标签
                }
            }
            else
            {
                string tagString = searchText.Trim();
                //todo 实例化标签
            }
        }
        private void onTagsSelect(object sender, TagSelectEventArgs e)
        {
            List<TagInfo> tagInfoList = e.TagInfoList;
            TagJar tagjar = new TagJar();//此处要通过Arg获得实际的Tag并添加到TagJar中，其中还有Tag大小到weight的转换
            Dictionary<Tag,Pair<long,long>> tagWeightList = new Dictionary<Tag,Pair<long,long>>();
            foreach (TagInfo info in tagInfoList)
            {
                info.IsSelected = true;
                //todo 移动到中央，大小做适当改变
                Tag tag = new Tag(info.TagType,info.TagValue);
                tagWeightList.Add(tag,new Pair<long,long>((long)info.Width,(long)info.Width));
                tagjar.Add(tag, (long)info.Width);
            }
            Thread searchTread = new Thread(delegate()
                {
                    if (CoronaSupport.State == SupportState.Ready)
                    {
                        //启动新线程搜索相关标签
                        Thread relateTagThread = new Thread(delegate()
                        {
                            TagJar resultTagJar = CoronaSupport.GetTagJar(tagjar, 0.8);
                            foreach (Pair<Tag, long> tagWeightPair in resultTagJar.Relations)
                            {
                                //todo 实例化标签
                            }
                        });
                        relateTagThread.Start();

                        Query query = QueryFactory.CreateQuery(tagWeightList, 0.8);
                        Resource<File>[] resultResource = CoronaSupport.getResources(CoronaSupport.Search(query));
                        foreach (Resource<File> resource in resultResource)
                        {
                            //todo 在文件显示面板上显示
                        }

                    }
                });
                searchTread.Start();
        }


        private void onSourceDragOn(object sender, SourceDragOnEventArgs e)
        {
            if (e.Type == SourceDragOnType.Link)
            {
                FileResource fileResource = new FileResource(e.TargetString);
                //todo 在cavas上面添加resource对象
            }
            else
            {
                string tagStr = e.TargetString.Trim();
                if (!Regex.IsMatch(tagStr,@"(\.|\\|\?|,|\(|\)|\{|\}|，|。|？|！|……|、|\*|\+|\||-|\r|\n|\t|\v|\f|\^| )"))
                    //todo 实例化标签
                    ;

            }
        }

        private void onSourceClick(object sender, EventArgs e)
        {
            FileResource resouce = new FileResource("");
            Thread searchTread = new Thread(delegate()
            {
                IEnumerable<Tag> tagList = CoronaSupport.GetTags(resouce);
                TagJar tagJar = new TagJar();
                //todo 清除cavas上的标签
                foreach (Tag tag in tagList)
                {
                    //todo 在cavas添加文件相关标签
                    if (tag.Name == "RelationShip")
                        //todo 直接实例化相关文件
                        ;
                    else
                        if (tag.Name == "Description")
                            tagJar.Add(tag, 100);
                }
                TagJar relatedTagjar = CoronaSupport.GetTagJar(tagJar, 0.8);
                foreach (Pair<Tag, long> relatedPair in relatedTagjar.Relations)
                {
                    Resource<File>[] resourceList = CoronaSupport.getResources(CoronaSupport.Search(relatedPair.A));
                    foreach (Resource<File> re in resourceList)
                        //todo 实例化相关文件
                        ;
                }
            });
        }
    }
}
